import pandas as pd
from test.shanghangju2 import get_content

excel_file = '/Users/yyq/code/simple-util-py/test/resources/专家库v2/局专家库信息.xlsx'

df = pd.read_excel(excel_file)
length = len(df)
list_ = []
last_index = 1
for i in range(2, length):
    id = df.iloc[i, 0]
    if pd.isna(id):
        continue
    else:
        list_.append(df.iloc[last_index:i].values)
        last_index = i
print("数据切分完成")
info_list: list[dict] = []
for info in list_:
    length = len(info)
    # 基础信息
    base_info = {
        'name': info[0][0],
        'gongsi': info[0][1],
        'zhiwu': info[0][2],
        'zhicheng': info[0][3],
    }

    scly_list = []  
    xffx_list = []  
    zyzgzs_list = [] 
    for i in range(length):
        info_ = info[i]
        if not pd.isna(info_[4]):
            scly_list.append(info_[4])
        if not pd.isna(info_[5]):
            xffx_list.append(info_[5])
        if not pd.isna(info_[6]):
            zyzgzs_list.append(info_[6])

    info_list.append({
        'base_info': base_info,
        'scly_list': scly_list,
        'xffx_list': xffx_list,
        'zyzg_list': zyzgzs_list,
    })
print("数据解析完成")
for i in range(len(info_list)):
    for values in info_list[i].values():
        if values:
            if type(values) == dict:
                for k, v in values.items():
                    if pd.isna(v):
                        values[k] = ''
            if type(values) == list:
                for i in range(len(values)):
                    for j in range(len(values[i])):
                        if pd.isna(values[i][j]):
                            values[i][j] = ''
print("数据清洗完成")
def shanghangju_post():
    from request_kms import post_rich_content_knowledge_detail
    cids = []
    kids = []
    cate = 1989
    for info in info_list:
        c, k = post_rich_content_knowledge_detail(kb_id=28,cate_parent_id=5849)
        cids.append(c)
        kids.append(k)
    print(cids)
    print(kids)


def shanghangju():
    from request_kms import (approve, put_rich_content_knowledge_detail)

    success = False
    ids = 2160 # 2160-2344
    for info in info_list:
        content = get_content.get_content_zhuanjia(info)
        success = put_rich_content_knowledge_detail(
            knowledge_id=ids,
            name=info['base_info']['name'],
            content=content,
            cate_id=ids)
        approve(ids)
        ids += 1


def devkms_post():
    from request_kms_test import post_rich_content_knowledge_detail
    cids = []
    kids = []
    for info in info_list:
        c, k = post_rich_content_knowledge_detail(cate_parent_id=75109)
        cids.append(c)
        kids.append(k)
    print(cids)
    print(kids)


def devkms():
    from request_kms_test import put_rich_content_knowledge_detail
    success = False
    cate_ids = [75115, 75116, 75117, 75118, 75119, 75120, 75121, 75122, 75123, 75124, 75125, 75126, 75127, 75128, 75129, 75130, 75131, 75132, 75133, 75134, 75135, 75136, 75137, 75138, 75139, 75140, 75141, 75142, 75143, 75144, 75145, 75146, 75147, 75148, 75149, 75150, 75151, 75152, 75153, 75154, 75155, 75156, 75157, 75158, 75159, 75160, 75161, 75162, 75163, 75164, 75165, 75166, 75167, 75168, 75169, 75170, 75171, 75172, 75173, 75174, 75175, 75176, 75177, 75178, 75179, 75180, 75181, 75182, 75183, 75184, 75185, 75186, 75187, 75188, 75189, 75190, 75191, 75192, 75193, 75194, 75195, 75196, 75197, 75198, 75199, 75200, 75201, 75202, 75203, 75204, 75205, 75206, 75207, 75208, 75209, 75210, 75211, 75212, 75213, 75214, 75215, 75216, 75217, 75218, 75219, 75220, 75221, 75222, 75223, 75224, 75225, 75226, 75227, 75228, 75229, 75230, 75231, 75232, 75233, 75234, 75235, 75236, 75237, 75238, 75239, 75240, 75241, 75242, 75243, 75244, 75245, 75246, 75247, 75248, 75249, 75250, 75251, 75252, 75253, 75254, 75255, 75256, 75257, 75258, 75259, 75260, 75261, 75262, 75263, 75264, 75265, 75266, 75267, 75268, 75269, 75270, 75271, 75272, 75273, 75274, 75275, 75276, 75277, 75278, 75279, 75280, 75281, 75282, 75283, 75284, 75285, 75286, 75287, 75288, 75289, 75290, 75291, 75292, 75293, 75294, 75295, 75296, 75297, 75298, 75299]
    knowledge_ids = [75503, 75504, 75505, 75506, 75507, 75508, 75509, 75510, 75511, 75512, 75513, 75514, 75515, 75516, 75517, 75518, 75519, 75520, 75521, 75522, 75523, 75524, 75525, 75526, 75527, 75528, 75529, 75530, 75531, 75532, 75533, 75534, 75535, 75536, 75537, 75538, 75539, 75540, 75541, 75542, 75543, 75544, 75545, 75546, 75547, 75548, 75549, 75550, 75551, 75552, 75553, 75554, 75555, 75556, 75557, 75558, 75559, 75560, 75561, 75562, 75563, 75564, 75565, 75566, 75567, 75568, 75569, 75570, 75571, 75572, 75573, 75574, 75575, 75576, 75577, 75578, 75579, 75580, 75581, 75582, 75583, 75584, 75585, 75586, 75587, 75588, 75589, 75590, 75591, 75592, 75593, 75594, 75595, 75596, 75597, 75598, 75599, 75600, 75601, 75602, 75603, 75604, 75605, 75606, 75607, 75608, 75609, 75610, 75611, 75612, 75613, 75614, 75615, 75616, 75617, 75618, 75619, 75620, 75621, 75622, 75623, 75624, 75625, 75626, 75627, 75628, 75629, 75630, 75631, 75632, 75633, 75634, 75635, 75636, 75637, 75638, 75639, 75640, 75641, 75642, 75643, 75644, 75645, 75646, 75647, 75648, 75649, 75650, 75651, 75652, 75653, 75654, 75655, 75656, 75657, 75658, 75659, 75660, 75661, 75662, 75663, 75664, 75665, 75666, 75667, 75668, 75669, 75670, 75671, 75672, 75673, 75674, 75675, 75676, 75677, 75678, 75679, 75680, 75681, 75682, 75683, 75684, 75685, 75686, 75687]
    index = 0
    open_ = False
    for info in info_list:
        content = get_content.get_content_zhuanjia(info)
        if open_:
            success = put_rich_content_knowledge_detail(
                knowledge_id=knowledge_ids[index],
                name=info['base_info']['name'],
                content=content,
                cate_id=cate_ids[index])
        index += 1
        if not open_ and info['base_info']['name']=="杜金洪":
            open_ = True
        if success:
            break
        # print(content)
        # break

def prod_shanghangju_post():
    from request_kms_prod import post_rich_content_knowledge_detail
    cids = []
    kids = []
    for info in info_list:
        c, k = post_rich_content_knowledge_detail(cate_parent_id=5849)
        cids.append(c)
        kids.append(k)
    print(cids)
    print(kids)


def prod_shanghangju():
    from request_kms_prod import (approve, put_rich_content_knowledge_detail)

    success = False
    ids = 5931 # 5931-6115
    for info in info_list:
        content = get_content.get_content_zhuanjia(info)
        success = put_rich_content_knowledge_detail(
            knowledge_id=ids,
            name=info['base_info']['name'],
            content=content,
            cate_id=ids)
        approve(ids)
        ids += 1
        # break

def prod_batch_approve_pass():
    from request_kms_prod import (approve_list,pass_approve)
    while True:
        ids = approve_list()
        if ids:
            for id_ in ids:
                pass_approve(approve_id=id_)
        else:
            break

print("发起执行")

# devkms_post()
# devkms()

# shanghangju_post()
# shanghangju()

# prod_shanghangju_post()
prod_shanghangju()
prod_batch_approve_pass()
